package com.bdqn.easybuy1.service.order;

import com.bdqn.easybuy1.dao.OrderDao;
import com.bdqn.easybuy1.dao.OrderDetailDao;
import com.bdqn.easybuy1.dao.ProductDao;
import com.bdqn.easybuy1.dao.impl.OrderDaoImpl;
import com.bdqn.easybuy1.dao.impl.OrderDetailDaoImpl;
import com.bdqn.easybuy1.dao.impl.ProductDaoImpl;
import com.bdqn.easybuy1.entities.Order;
import com.bdqn.easybuy1.entities.OrderDetail;
import com.bdqn.easybuy1.entities.User;
import com.bdqn.easybuy1.utils.JDBCUtils;
import com.bdqn.easybuy1.utils.ShoppingCart;
import com.bdqn.easybuy1.utils.ShoppingCartItem;
import com.bdqn.easybuy1.utils.StringUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    /**
     * 结算购物车物品包含以下步骤：
     * 1.生成订单
     * 2.生成订单明细
     * 3.更新商品表，减库存
     * 注意加入事物的控制
     */
    @Override
    public Order payShoppingCart(ShoppingCart cart, User user, String address) {
        Connection connection = null;
        Order order = new Order();
        try {
            connection = JDBCUtils.getConnection();
            connection.setAutoCommit(false);
            ProductDao productDao = new ProductDaoImpl();
            OrderDao orderDao = new OrderDaoImpl();
            OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
            //增加订单
            order.setUserId(user.getId());
            order.setLoginName(user.getLoginName());
            order.setUserAddress(address);
            order.setCreateTime(new Date(new java.util.Date().getTime()));
            order.setCost(cart.getTotalCost());
            order.setSerialNumber(StringUtils.randomUUID());
            orderDao.insert(connection, order);

            //增加订单对应的明细信息
            for (ShoppingCartItem item : cart.getItems()) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderId(order.getId());
                orderDetail.setCost(item.getCost());
                orderDetail.setProduct(item.getProduct());
                orderDetail.setQuantity(item.getQuantity());
                orderDetailDao.insert(connection, orderDetail);
                //更新商品表的库存
                productDao.updateStock(connection, item.getProduct().getId(), item.getQuantity());
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            order = null;
        } finally {
            try {
                connection.setAutoCommit(true);
                DbUtils.closeQuietly(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return order;
    }

    @Override
    public List<Order> getOrderList(Integer userId, Integer currentPageNo, Integer pageSize) {
        Connection connection = null;
        List<Order> orderList = new ArrayList<Order>();
        try {
            connection = JDBCUtils.getConnection();
            OrderDao orderDao = new OrderDaoImpl();
            orderList = orderDao.getOrderList(connection, userId, currentPageNo, pageSize);
            for (Order order : orderList) {
                order.setOrderDetailList(getOrderDetailList(order.getId()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return orderList;
        }
    }

    @Override
    public int count(Integer userId) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            OrderDao orderDao = new OrderDaoImpl();
            count = orderDao.getOrderCount(connection, userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count;
        }
    }

    @Override
    public List<OrderDetail> getOrderDetailList(Integer orderId) {
        Connection connection = null;
        List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
        try {
            connection = JDBCUtils.getConnection();
            OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
            ProductDao productDao = new ProductDaoImpl();
            orderDetailList = orderDetailDao.getOrderDetailList(connection, orderId);
            for (OrderDetail orderDetail : orderDetailList) {
                orderDetail.setProduct(productDao.getProductById(connection,orderDetail.getProductId()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return orderDetailList;
        }
    }
}
